package cn.ch.train.business.service;

import cn.ch.train.business.domain.Station;
import cn.ch.train.business.domain.StationExample;
import cn.ch.train.business.enums.BusinessExceptionEnum;
import cn.ch.train.business.mapper.StationMapper;
import cn.ch.train.business.request.StationQueryParam;
import cn.ch.train.business.request.StationSaveParam;
import cn.ch.train.business.response.StationQueryResult;
import cn.ch.train.common.exception.BusinessException;
import cn.ch.train.common.response.PageResult;
import cn.ch.train.common.util.SnowUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class StationService {
    private static final Logger logger = LoggerFactory.getLogger(StationService.class);
    @Resource
    private StationMapper stationMapper;

    public void save(StationSaveParam param) {
        DateTime now = DateTime.now();
        Station station = BeanUtil.copyProperties(param, Station.class);
        // 保存之前，先校验唯一键是否存在
        Station stationDb = selectByUnique(param.getName());
        if (Objects.nonNull(stationDb)) {
            throw new BusinessException(BusinessExceptionEnum.STATION_HAS_EXIST.getDesc());
        }
        if (Objects.isNull(station.getId())) {
            station.setId(SnowUtil.getSnowFlakeNextId());
            station.setCreateTime(now);
            station.setUpdateTime(now);
            stationMapper.insert(station);
        } else {
            station.setUpdateTime(now);
            stationMapper.updateByPrimaryKey(station);
        }
    }

    private Station selectByUnique(String name) {
        StationExample stationExample = new StationExample();
        StationExample.Criteria criteria = stationExample.createCriteria();
        criteria.andNameEqualTo(name);
        List<Station> stationList = stationMapper.selectByExample(stationExample);
        if (CollUtil.isNotEmpty(stationList)) {
            return stationList.get(0);
        }
        return null;
    }

    public PageResult<StationQueryResult> queryList(StationQueryParam param) {
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("id desc");
        StationExample.Criteria criteria = stationExample.createCriteria();
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        List<Station> stationList = stationMapper.selectByExample(stationExample);

        PageInfo<Station> stationPageInfo = new PageInfo<>(stationList);
        logger.info("总行数：{}", stationPageInfo.getTotal());
        logger.info("总页数：{}", stationPageInfo.getPages());
        List<StationQueryResult> stationQueryList = BeanUtil.copyToList(stationList, StationQueryResult.class);
        PageResult<StationQueryResult> pageResult = new PageResult<>();
        pageResult.setTotal(stationPageInfo.getTotal());
        pageResult.setList(stationQueryList);
        return pageResult;
    }

    public void delete(Long id) {
        stationMapper.deleteByPrimaryKey(id);
    }

    public List<StationQueryResult> queryAll() {
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("name_pinyin asc");
        List<Station> stationList = stationMapper.selectByExample(stationExample);

        List<StationQueryResult> stationQueryList = BeanUtil.copyToList(stationList, StationQueryResult.class);
        return stationQueryList;
    }
}
